"use strict"

let ItemComponent = require('./itemComponent')
let templateConfig = require('../../config/templateConfig')
let totalConfig = templateConfig.total

//包裹类
class PackageComponent {
  constructor(opt) {
    this.size = opt.size || totalConfig[290030].free //包裹大小
    this.packageType = opt.packageType || 1 //包裹类型
    this.items = opt.items || new Array(this.size) //背包数组 [itemComponent1,itemComponent2,....]
    this.itemsById = {} //物品列表集合，键为itemId=>[itemComponent1,itemComponent2]
    this.emptySize = this.size //空格子数量
    this.initPackag()
  }

  //初始化背包
  initPackag() {
    this.emptySize = this.size
    for (let i in this.items) {
      let itemComponent = this.items[i]
      if (!itemComponent) {
        continue
      }
      let templateId = itemComponent.templateId
      itemComponent = ItemComponent.getPropComponent(itemComponent)
      this.items[i] = itemComponent
      itemComponent.pos = parseInt(i)
      if (!this.itemsById[templateId]) {
        this.itemsById[templateId] = []
      }
      this.itemsById[templateId].push(itemComponent)
      this.emptySize -= 1 //空格子数量
    }
  }

  //获取背包信息
  getInfo() {
    return {
      items: this.items,
      size: this.size,
      emptySize: this.emptySize
    }
  }

  //获取背包大小
  getSize() {
    return this.size
  }

  //获取指定位置的物品以及物品的位置信息
  getItemByPos(pos) {
    return this.items[pos]
  }

  //添加物品
  addItem(itemComponent) {
    this.initPackag()
    //判断背包是否有空
    if (this.emptySize === 0) {
      return -1
    }
    //判断物品是否可以叠加
    let maxstack = totalConfig[itemComponent.templateId].maxstack
    if (maxstack === 1) {
      //不可叠加，添加到空格子
      this._addItemToPosition(itemComponent)
      return 0
    }
    //查找物品是否已经存在
    let itemInfoId = this.itemsById[itemComponent.templateId]
    if (!itemInfoId) {
      //不存在，添加到空格子
      this._addItemToPosition(itemComponent)
    } else {
      let num = itemComponent.stack //叠加数
      for (let i in itemInfoId) {
        let p = itemInfoId[i]
        let stack = p.stack
        if (itemComponent.bind === p.bind) { //是否都是绑定或不绑定
          if (stack + num <= maxstack) {
            p.stack = stack + num
            num = 0
            return 0
          } else {
            num = stack + num - maxstack
            p.stack = maxstack
          }
        }
      }
      if (num > 0) {
        //剩余的数量添加到空格子
        itemComponent.stack = num
        this._addItemToPosition(itemComponent)
      }
    }
    return 0
  }

  //添加物品到空格子
  _addItemToPosition(itemComponent) {
    let pos = this._getEmpty() //获取空格子的下标
    if (pos === -1) {
      return -1
    }
    itemComponent.pos = pos
    this.emptySize -= 1 //空格子数量减一
    this.items[pos] = itemComponent
    let itemInfoId = this.itemsById[itemComponent.templateId]
      //查找物品是否已经存在
    if (itemInfoId) {
      itemInfoId.push(itemComponent)
    } else {
      this.itemsById[itemComponent.templateId] = [itemComponent]
    }
  }

  //获取一个空格子的下标
  _getEmpty() {
    if (this.emptySize === 0) {
      return -1
    }
    for (let i = 0; i < this.size; i++) {
      if (!this.items[i]) {
        return i
      }
    }
  }

  //交换物品的位置
  transPositionItems(pos, topos) {
    let fromItemComponent = this.items[pos]
    let toItemComponent = this.items[topos]
    this.items[pos] = toItemComponent
    this.items[topos] = fromItemComponent
    toItemComponent.pos = topos
    fromItemComponent.pos = pos
    let fromItemInfoById = this.itemsById[fromItemComponent.templateId]
      /*  fromItemInfoById.sort(function(itemComponent1, itemComponent2) { //排序
          return itemComponent1.pos > itemComponent2.pos ? 1 : -1
        })
        let toItemInfoById = this.itemsById[toItemComponent.templateId]
        toItemInfoId.sort(function(itemComponent1, itemComponent2) { //排序
          return itemComponent1.pos > itemComponent2.pos ? 1 : -1
        })*/
  }

  //移除包裹中的物品
  removeItemByItemId(itemId, num) {
    this.initPackag()
    let itemInfoById = this.itemsById[itemId]
    for (let i = itemInfoById.length - 1; i >= 0; i--) {
      let p = itemInfoById[i]
      let stack = p.stack
      if (stack > num) {
        p.stack -= num
        return 0
      } else if (stack === num) {
        this.removeItemByPosition(p.pos)
        return 0
      } else {
        num -= p.stack
        this.removeItemByPosition(p.pos)
      }
    }
    return 0
  }

  //根据下标删除背包中的物品
  removeItemByPosition(pos) {
    this.emptySize += 1 //空格子加一
    let itemId = this.items[pos].templateId
    this.items[pos] = null

    let itemInfoById = this.itemsById[itemId]
    if (itemInfoById.length === 1) {
      delete this.itemsById[itemId]
    } else {
      for (let i in itemInfoById) {
        if (itemInfoById[i].pos === pos) {
          itemInfoById.splice(i, 1)
          break
        }
      }
    }
  }

  removeSomeItemByPosition(pos, num) {
    if (this.items[pos].stack === num) {
      this.removeItemByPosition(pos)
    } else if (this.items[pos].stack < num) {
      return -1
    } else {
      this.items[pos].stack -= num
    }
    return 0
  }

  //获取某一物品的总数量
  getTotalNumByItemId(itemId) {
    let itemInfoId = this.itemsById[itemId]
      //查找物品是否已经存在
    if (!itemInfoId) {
      return 0
    }

    let total = 0
    for (let p of itemInfoId) {
      total += p.stack
    }
    return total
  }

  //合并物品
  mergeItems(frompos, topos) {
    if (frompos === topos) {
      return 1 //位置一致，不能合并
    }
    let fromItemComponent = this.items[frompos]
    let toItemComponent = this.items[topos]
    if (!fromItemComponent || !toItemComponent) {
      return 1 //有物品不存在不能合并
    }

    if (fromItemComponent.templateId !== toItemComponent.templateId) {
      return 1 //物品类型不同不能合并
    }

    if (fromItemComponent.bind !== toItemComponent.bind) {
      return 1 //物品类型交易类型不一样
    }

    let maxstack = totalConfig[fromItemComponent.templateId].maxstack
    let fromstack = fromItemComponent.stack
    let tostack = toItemComponent.stack
    if (fromstack === maxstack) {
      return 0 //起始位置物品堆叠数达到上限，不能进行合并
    }

    if (tostack === maxstack) {
      //置换
      this.transPositionItems(frompos, topos)
      return 0
    }

    if (tostack + fromstack < maxstack) {
      fromItemComponent.stack = tostack + fromstack
      this.removeItemByPosition(topos)
    } else {
      let startstack = maxstack
      let endstack = tostack + fromstack - maxstack
      fromItemComponent.stack = startstack
      toItemComponent.stack = endstack
    }
  }

  //合并包裹中能合并的物品
  mergePackage() {
    for (let i = 0; i < this.size - 1; i++) {
      for (let j = i + 1; j < this.size; j++) {
        this.mergeItems(i, j)
      }
    }
  }

  //清空包裹中的物品
  clearPackage() {
    this.itemsById = {}
    this.items = []
    this.emptySize = this.size
  }

  //包裹整理
  arrangePackage() {
    this.mergePackage() //先合并所有的物品
      //items排序
    this.items.sort(function(item1, item2) {
      if (item1 && item2) {
        if (item1.templateId === item2.templateId) {
          return item1.stack >= item2.stack ? 1 : -1
        } else {
          return item1.templateId > item2.templateId ? 1 : -1
        }
      } else if (!item1 && item2) {
        return 1
      } else if (item1 && !item2) {
        return -1
      } else {
        return 1
      }
    })
    this.initPackag()
  }
}

module.exports = PackageComponent
